<?php
/**
 * Created by PhpStorm.
 * User: vanity
 * Date: 2018/1/2
 * Time: 13:04
 */
function changeUrl($str)
{
    $path = implode('/',explode('.',$str));
    $url = U($path);
    return $url;
}
function getRoles($id)
{
    $role = M('roles')->field('id,name')->select();
    $html = '<select class="form-control" name="role_id"><option value="">--请选择--</option>';
    foreach($role as $v){
        if($id == $v['id']){
            $html .= '<option value="'.$v['id'].'" selected="selected">'.$v['name'].'</option>';
        }else{
            $html .= '<option value="'.$v['id'].'">'.$v['name'].'</option>';
        }
    }
    $html .= '</select>';
    return $html;
}
function getPermission($permission_role)
{
    if(!empty($permission_role)){
        foreach($permission_role as $v){
            $permission[] = $v['permission_id'];
        }
    }
    $field = 'name,id,cid,label';
    $top = M('permissions')->field($field)->where('cid = 0')->select();
    $down = M('permissions')->field($field)->where('cid <> 0')->select();
    foreach($down as $val){
        $data[$val['cid']][] = $val;
    }
    $str = '';
    foreach($top as $value){
        $str .='<label>'.$value['label'].':</label>';
        foreach($data[$value['id']] as $vo){
            if(in_array($vo['id'],$permission)){
                $str .='<input type="checkbox" class="flat-red" name="permission_id[]" value="'.$vo['id'].'" checked>'.$vo['label'];
            }else{
                $str .='<input type="checkbox" class="flat-red" name="permission_id[]" value="'.$vo['id'].'">'.$vo['label'];
            }
        }
        $str .= '<br/>';
    }
    return $str;
}
function setImage($image)
{
    if(empty($image)){
        return '/user.jpg';
    }
    return $image;
}